﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using GalaSoft.MvvmLight.Messaging;
using HP.Cachalote.Services.Interfaces;
using HP.Cachalote.WPF.Model;

namespace HP.Cachalote.WPF.Shared
{
    public abstract class MyViewModelBase : ViewModelBase, IDisposable
    {
        public const string RequestDisplayErrorMessageToken = "RequestDisplayErrorMessage";
        public const string RequestClearErrorMessageToken = "RequestClearErrorMessage";
        public const string RequestDisplayMessageToken = "RequestDisplayMessage";
        public const string RequestClearMessageToken = "RequestClearMessage";

        protected IServiceFactory ServiceFactory;
        protected IDataService CurrentDataService;

        public MyViewModelBase(IDataService dataService)
        {
            CurrentDataService = dataService;

            dataService.GetServiceFactory(
                (item, error) =>
                {
                    if (error != null)
                    {
                        // Report error here
                        return;
                    }

                    ServiceFactory = item;
                });

            RegisterMessages();
        }

        public virtual void Dispose()
        {
            Cleanup();
        }

        /// <summary>
        /// Register all necessary messages.
        /// </summary>
        protected virtual void RegisterMessages() { }

        /// <summary>
        /// Request SummaryMessageDisplayViewModel to display a provided error message.
        /// </summary>
        /// <param name="errorMessage">Provided error message.</param>
        protected virtual void RequestDisplayErrorMessage(string errorMessage)
        {
            Messenger.Default.Send<string>(errorMessage, RequestDisplayErrorMessageToken);
        }

        /// <summary>
        /// Request SummaryMessageDisplayViewModel to clear error message.
        /// </summary>
        protected virtual void RequestClearErrorMessage()
        {
            Messenger.Default.Send<string>(string.Empty, RequestClearErrorMessageToken);
        }

        /// <summary>
        /// Request SummaryMessageDisplayViewModel to display a provided message.
        /// </summary>
        /// <param name="message">Provided message.</param>
        protected virtual void RequestDisplayMessage(string message)
        {
            Messenger.Default.Send<string>(message, RequestDisplayMessageToken);
        }

        /// <summary>
        /// Request SummaryMessageDisplayViewModel to clear message.
        /// </summary>
        protected virtual void RequestClearMessage()
        {
            Messenger.Default.Send<string>(string.Empty, RequestClearMessageToken);
        }
    }
}
